package com.itheima.web;

import com.itheima.utils.CheckCodeUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/validateCode")
public class ValidateCodeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("来了一个GET请求...");

        //1. 构建验证码图片:: 把图片写出去，并且返回验证码
        String code = CheckCodeUtil.outputVerifyImage(120, 50, resp.getOutputStream(), 4);

        //2. 把验证码保存起来，以便一会注册的时候，能够用来比较。
        req.getSession().setAttribute("code", code);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("来了一个POST请求...");
        this.doGet(req, resp);
    }
}
